﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PluginInterface;
using Buccontrols;

namespace Buccaneer
{
    public partial class Daddy : Form
    {
        public Daddy(PluginServices ph)
        {
            InitializeComponent();

            this.Plugins = ph;
            this.Plugins.RegisterAction("startdragging", new ActionFunction(StartDragging));
            this.tabs = new List<TabSpace>();
            this.afnames = new List<string>();
            this.afargs = new List<string[]>();

            foreach (var c in Controls)
            {
                if (c is MdiClient)
                {
                    this.mdiParentArea = c as MdiClient;
                    break;
                }
            }
        }

        List<TabSpace> tabs;
        List<string> afnames;
        List<string[]> afargs;
        DragForm dragForm;
        MdiClient mdiParentArea;

        public PluginServices Plugins;

        #region Public Functions

        public void AddTab(string name, Control parent)
        {
            TabSpace ts = new TabSpace();
            ts.Dock = DockStyle.Fill;
            ts.Name = name;
            ts.Parent = parent;
            foreach (TabSpace tab in tabs)
            {
                tab.Visible = false;
            }
            this.Controls.Add(ts);
        }

        public void RemoveTab(string name)
        {
            foreach (TabSpace tab in tabs)
            {
                if (tab.Name == name)
                {
                    this.tabs.Remove(tab);
                    break;
                }
            }
        }

        public void AddPlugin(string name)
        {
            Form frm = new Form();
            frm.Name = name;
            frm.MdiParent = this;
            frm.Show();
        }

        public void RemovePlugin(string name)
        {
            foreach (Form form in this.MdiChildren)
            {
                if (form.Name == name)
                {
                    form.Close();
                    break;
                }
            }
        }

        public void ActRequest(string afname, string[] args)
        {
            this.afnames.Add(afname);
            this.afargs.Add(args);
        }

        public void HandleActRequests()
        {
            for (int i = 0; i < afnames.Count; i++)
            {
                Plugins.Act(afnames[i], afargs[i]);
            }
            afnames = null;
            afargs = null;
            afnames = new List<string>();
            afargs = new List<string[]>();
        }

        public void StartDragging(string[] args)
        {
            string name = args[0];

            foreach (Form form in this.MdiChildren)
            {
                if (form.Name == name)
                {
                    form.SuspendLayout();
                    form.Visible = false;
                    form.StartPosition = FormStartPosition.Manual;
                    dragForm = new DragForm(
                        new Rectangle(this.mdiParentArea.PointToScreen(new Point(0, 0)), this.mdiParentArea.ClientSize), 
                        this.mdiParentArea.PointToScreen(form.Location),
                        form.Size
                    );
                    dragForm.ShowDialog();
                    switch (dragForm.Result)
                    {
                        case DragFormResult.Return:
                            form.Location = this.mdiParentArea.PointToClient(dragForm.Location);
                            form.Size = dragForm.Size;
                            form.Visible = true;
                            form.ResumeLayout(false);
                            break;
                        case DragFormResult.Close:
                            form.Close();
                            break;
                        case DragFormResult.Options:
                            form.Location = this.mdiParentArea.PointToClient(dragForm.Location);
                            form.Size = dragForm.Size;
                            form.Visible = true;
                            form.ResumeLayout(false);
                            break;
                        case DragFormResult.Add:
                            this.Plugins.Act("addform", null);
                            form.Location = this.mdiParentArea.PointToClient(dragForm.Location);
                            form.Size = dragForm.Size;
                            form.Visible = true;
                            form.ResumeLayout(false);
                            break;
                        default:
                            form.Visible = true;
                            form.ResumeLayout(false);
                            break;
                    }
                    
                    break;
                }
            }
        }

        #endregion

        private void Daddy_Activated(object sender, EventArgs e)
        {
            HandleActRequests();
        }
    }
}
